<?php
/**
 *	Posts Controller - This is for all methods related to posts on
 *	the webrocket site
 *
 *	@package webrocket
 *	@subpackage webrocket.app.controllers.posts
 */
class PostsController extends AppController
{
	var $name = 'Posts';
	var $uses = array('Post','Comment','Subject','Copyright');
	var $paginate = array('fields'=>array('Post.id', 'Post.title', 'Post.byline', 'Post.body', 'Post.user_id', 'Post.url' ,'Post.created' , 'Post.published' ,'User.id', 'User.username'),'limit'=>5, 'order'=>array('Post.created'=>'DESC'));
	
	//var $recursion = 0;
	
	//var $cacheAction = "1 hour";
	
	/**
	 *	Function index is for the main front page
	 */
	function index()
	{
		//$posts = $this->Post->findAll('Post.published = 1', array('Post.id','Post.url','Post.title', 'Post.byline', 'Post.body', 'Post.created', 'Post.modified', 'User.id', 'User.username'), 'Post.created DESC', 2);
		//$this->set('posts', $posts);
		
		// Overide paginate limit, may need to change
		$this->passedArgs['limit'] = 1;
		$posts = $this->paginate();
		$this->set(compact('posts'));
		
		$num_comments = array();	
		foreach ($posts as $post) :
			$num_comments[$post['Post']['id']] =  $this->Comment->findCount('Comment.post_id = ' . $post['Post']['id']);
		endforeach;
		$this->set(compact('num_comments'));
	}
	
	/**
	 * Function news is for the main news page, shows more posts
	 */
	function news($show = 5)
	{
		// Overide paginate limit, may need to change
		$this->passedArgs['limit'] = $show;
		
		$posts = $this->paginate();
		$this->set(compact('posts'));
		
		$num_comments = array();
		foreach ($posts as $post) :
			$num_comments[$post['Post']['id']] =  $this->Comment->findCount('Comment.post_id = ' . $post['Post']['id']);
		endforeach;
		$this->set(compact('num_comments'));
	}
	
	/**
	 * Function view is to show individual posts
	 * @string url
	 */
	function view($url)
	{
    	$this->set('post', $this->Post->findByUrl($url));
	}
	
	/**
	 * Function getposts grabs x posts from database
	 * @int num
	 */
	function getposts($num = 5)
	{
		$upper = $num + 2;
		$getposts = $this->Post->findAll('Post.published = 1', array('Post.id','Post.url','Post.title', 'Post.byline','User.username'), 'Post.created DESC', '2,' . $upper);
		if(isset($this->params['requested'])) {
			return $getposts;
		}
		$this->set('getposts', $getposts);
	}
	
	/**
	 * Function feed creates an RSS feed
	 */
	function feed()
    {
    	$this->layout = 'xml';
		$posts = $this->Post->findAll('Post.published = 1', array('Post.id','Post.url','Post.title', 'Post.byline', 'Post.body', 'Post.created', 'Post.modified', 'User.id', 'User.username'), 'Post.created DESC', 15);
		$this->set('posts', $posts);
    }
	
	function json()
	{
		$this->layout = 'json';
		$posts = $this->Post->findAll(null, array('Post.id','Post.url','Post.title', 'Post.byline', 'Post.body', 'Post.created', 'Post.modified', 'User.id', 'User.username'), 'Post.created DESC');
		$num_posts = $this->Post->findCount();
		
		$num = 0;
		foreach ($posts as $post)
		{
			$myjson[$num]['id'] = $post['Post']['id'];
			$myjson[$num]['title'] = $post['Post']['title'];
			$myjson[$num]['url'] = $post['Post']['url'];
			$myjson[$num]['byline'] = $post['Post']['byline'];
			$myjson[$num]['created'] = $post['Post']['created'];
			$myjson[$num]['user_id'] = $post['User']['id'];
			$myjson[$num]['username'] = $post['User']['username'];
			$num++;		
		}
		
		$output = '({"total":"'.$num_posts.'","results":'.json_encode($myjson).'})';
		print_r($output);
		//$this->set('posts', $output);	
	}
	
	/**
	 * Function admin index lists 10 items per page with admin functions
	 */
	function admin_index()
	{		
		$posts = $this->paginate();
		$this->set(compact('posts'));
	}
	
	/**
	 * Function admin add allows the creation of posts
	 */
	function admin_add()
	{
		$copyright_type = $this->Copyright->generateList();
		$this->set(compact('copyright_type'));
		
		$this->set('author_id', $this->othAuth->user('id'));
		$subject = $this->Post->Subject->generateList();
		$this->set(compact('subject'));
		
		if (!empty ($this->data))
		{
			if ($this->Post->save($this->data))
			{
				$this->Session->setFlash('Post successfuly saved');
				$this->redirect('/posts/index/');
			} else {
				$this->Session->setFlash('Error in post, check below');
			}
		}
	}
	
	/**
	 * Function admin edit allows posts to be edited
	 * @int id
	 */
	function admin_edit($id)
	{
		if (!empty($this->data))
		{
			if ($this->Post->save($this->data['Post']))
        	{
            	$this->Session->setFlash('Your post has been updated.');
				$this->redirect('/admin/posts/index');
        	}
		} else {
			$this->Post->id = $id;
        	$this->data = $this->Post->read();
			$subject = $this->Post->Subject->generateList();
			$this->set(compact('subject'));
		}
	}
	
	/**
	 * Function admin delete removes the post from the system
	 * @int id
	 */
	function admin_delete($id)
	{
		$this->Post->delete(array('Post.id'=>$id));
		$this->Session->setFlash('Post ' . $id . ' Deleted.');
		$this->redirect('/admin/posts/');
	}
}
?>